package com.voltmao.service.application.intent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.voltmao.persistent.mapper.IntentUserAgainMapper;
import com.voltmao.persistent.mapper.IntentUserMapper;
import com.voltmao.persistent.model.IntentUserAgain;
import com.voltmao.persistent.model.IntentUserAgainCriteria;
import com.voltmao.webpro.frame.dto.DataPage;
import com.voltmao.webpro.utils.ParamsUtil;
import com.voltmao.webpro.utils.PropertiesUtils;
import com.voltmao.webpro.utils.StringUtils;

import java.util.*;

/**
 * @author zya
 * 2018/04/15
 * 跟进记录服务层
 */
@Service
public class IntentUserAgainServiceImpl {

    @Autowired
    IntentUserAgainMapper intentUserAgainMapper;
    @Autowired
    IntentUserMapper intentUserMapper;

    /**
     * 新增或更新跟进记录
     * @param params
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int insertOrUpdate(Map<String,Object> params) throws RuntimeException{
        //检查参数完整性
        String[] checkParams = {"forIntentUser","forCorpEmployesUser","des","visitDate"};
        ParamsUtil.checkNotNull(params,checkParams);

        IntentUserAgain intentUserAgain = (IntentUserAgain)PropertiesUtils.mapToObject(params,IntentUserAgain.class);

        if (StringUtils.isEmpty(intentUserAgain.getDataId())) {
            intentUserAgain.setCreateDate(new Date());
            intentUserAgain.setUpdateDate(new Date());
            //默认启用
            intentUserAgain.setIsDel(0);
            intentUserAgain.setDataId(StringUtils.uuid36());
            List<String> intentArr = new ArrayList<>();
            intentArr.add(intentUserAgain.getForIntentUser());
            //更新意向渠道状态值，跟进中
            intentUserMapper.updateYxyhzt(intentArr,"2");
            return intentUserAgainMapper.insert(intentUserAgain);
        } else {
            intentUserAgain.setUpdateDate(new Date());
            return intentUserAgainMapper.updateByPrimaryKeySelective(intentUserAgain);
        }

    }

    /**
     * 获取意向渠道跟进记录
     * @param params
     * @return
     */
    public DataPage getVisitRecodes(Map<String,Object> params) throws RuntimeException{
        String[] checkParams = {"intentUserId"};
        ParamsUtil.checkNotNull(params,checkParams);
        DataPage dataPage = new DataPage();
        int pageIdx = StringUtils.parseInt(params.get("pageNumber") + "", 1);
        int pageSize = StringUtils.parseInt(params.get("pageSize") + "", 20);

        IntentUserAgainCriteria E = new IntentUserAgainCriteria();
        IntentUserAgainCriteria.Criteria C = E.createCriteria();
        C.andForIntentUserEqualTo(params.get("intentUserId")+"");

        dataPage.pageindex = pageIdx;
        dataPage.pagesize = pageSize;
        dataPage.rows = intentUserAgainMapper.selectByExample(E);
        dataPage.total = intentUserAgainMapper.countByExample(E);
        return dataPage;
    }
}
